package com.readfeed.app;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import android.app.ActionBar;
import android.app.Activity;
import android.app.FragmentManager;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

import com.readfeed.app.data.FeedsAdapter;
import com.readfeed.app.fragment.ErrorDialogFragment;
import com.readfeed.app.model.Feed;
import com.readfeed.app.utils.Utilities;

public class FeedItemActivity extends Activity {

	private FeedsAdapter mFeedAdapter;
	private Feed mFeed;
	private String mUrl;
	private FragmentManager mFragmentManager;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_feed_item);
		mFeedAdapter = new FeedsAdapter(this);
		mFeedAdapter.open();
		long feedId = getIntent().getExtras().getLong(FeedsListActivity.FEED_ID_EXTRA);
		mFeed = this.getFeed(feedId);
		
		setupActionBar();		
		mFragmentManager = getFragmentManager();		
		mUrl = mFeed.getUrl();
		
		this.configureContent();
	}
	
	private Feed getFeed(long feedId){
		try {				
			Feed requestedFeed = mFeedAdapter.getFeedById(feedId);
			return requestedFeed;
		} catch (ParseException e) {
			ErrorDialogFragment errorDialog = ErrorDialogFragment.newInstance(R.string.error_fetching_feed);
			errorDialog.show(mFragmentManager, Utilities.ERROR_DIALOG_TAG);
		}
		
		return null;
	}
	
	private void configureContent(){
		TextView title = (TextView) findViewById(R.id.feed_item_title);
		TextView pubDate = (TextView) findViewById(R.id.feed_item_pubdate);
		TextView content = (TextView) findViewById(R.id.feed_item_content);
		
		String feedTitle = mFeed.getTitle();
		String feedContent = mFeed.getSummary();
		Date feedPubDate = mFeed.getPubDate();
		String dateString = this.dateToString(feedPubDate);
		
		if (feedTitle != null) {
			title.setText(feedTitle);
		}
		
		if (feedContent != null) {
			content.setText(feedContent);
		}
		
		if (dateString != null) {
			pubDate.setText(dateString);
		}
	}
	
	private String dateToString(Date date){
		SimpleDateFormat df = new SimpleDateFormat(getResources().getString(R.string.pubdate_format_pattern), Locale.US);
		String dateString = df.format(date);
		return dateString;
	}
	

	/**
	 * Set up the {@link android.app.ActionBar}.
	 */
	private void setupActionBar() {
		ActionBar actionBar = getActionBar();
		actionBar.setDisplayHomeAsUpEnabled(true);
		String title = mFeed.getTitle();
		actionBar.setTitle(title);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.feed_item, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			NavUtils.navigateUpFromSameTask(this);
			break;
			
		case R.id.action_read_online:
			Intent intent = new Intent(Intent.ACTION_VIEW);
			intent.setData(Uri.parse(mUrl));
			startActivity(intent);
			break;
			
		case R.id.action_share:			
			Intent shareIntent = new Intent();
        	shareIntent.setAction(Intent.ACTION_SEND);
        	shareIntent.putExtra(Intent.EXTRA_TEXT, mUrl);
        	shareIntent.setType("text/plain");
        	startActivity(shareIntent);
        	break;
        	
		case R.id.item_action_settings:
			Intent prefIntent = new Intent(FeedItemActivity.this, PreferencesActivity.class);
			startActivity(prefIntent);			
			break;			
		}
		
		return super.onOptionsItemSelected(item);
	}
}
